package com.caishi.lkx.user.api;

import cn.hutool.core.util.StrUtil;

import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.user.model.SysDictDetailModel;
import com.caishi.lkx.user.model.SysDictTypeModel;
import com.caishi.lkx.user.service.ISysDictDetailService;
import com.caishi.lkx.user.service.ISysDictTypeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "热词相关接口")
@RestController
@RequestMapping("/user/sys/dict")
public class SysDictApi extends AdminBaseApi<ISysDictTypeService, SysDictTypeModel, String> {

    @Resource
    private ISysDictTypeService sysDictTypeService;
    @Resource
    private ISysDictDetailService sysDictDetailService;

    @Override
    public ISysDictTypeService loadService() {
        return sysDictTypeService;
    }

    @DeleteMapping("/type/delete")
    public Boolean typeDelete(@RequestParam String id) {
        SysDictTypeModel byId = sysDictTypeService.getById(id);
        if (null == byId) {
            return false;
        }
        loadService().delete(id);
        sysDictDetailService.deleteByTypeCode(byId.getCode());
        return true;
    }

    @DeleteMapping("/type/deleted-list")
    public Boolean typedelete(@RequestParam List<String> ids) {
        ids.parallelStream().forEach(id -> {
            SysDictTypeModel byId = sysDictTypeService.getById(id);
            if (null == byId) {
                return;
            }
            loadService().delete(id);
            sysDictDetailService.deleteByTypeCode(byId.getCode());
        });
        return true;
    }
    @DeleteMapping("/detail/delete")
    public Boolean detailDelete(@RequestParam String id) {
        return sysDictDetailService.delete(id);
    }

    @DeleteMapping("/detail/deleted-list")
    public Boolean detaildelete(@RequestParam List<String> ids) {
        return sysDictDetailService.delete(ids);
    }


    @PostMapping("/detail/update")
    public SysDictDetailModel detailAdd(@ModelAttribute SysDictDetailModel entity) {
        return sysDictDetailService.insert(entity);
    }
    @PutMapping("/detail/update")
    public SysDictDetailModel detailUupdate(@ModelAttribute SysDictDetailModel entity) {
        return sysDictDetailService.simpleUpdate(entity);
    }

    @GetMapping("/detail/detail/{id}")
    public SysDictDetailModel detailDdetail(@PathVariable String id) {
        return sysDictDetailService.getById(id);
    }

    @GetMapping("/detail/listByTypeCode")
    public  List<SysDictDetailModel> getDetaiListByTypeCode(@RequestParam String typeCode,@RequestParam(required = false) String parentCode) {
        List<SysDictDetailModel> sysDictDetailModels = sysDictDetailService.selectByTypeCode(typeCode);
        if (StrUtil.isNotBlank(parentCode)){
            return sysDictDetailModels.parallelStream().filter(c -> parentCode.equals(c.getParentCode())).collect(Collectors.toList());
        }
        return sysDictDetailModels;
    }

    @PostMapping("/detail/detail-typeAndCode")
    public  SysDictDetailModel typeAndCode(@RequestParam String typeCode,@RequestParam String code) {
       return   sysDictDetailService.selectByTypeAndCode(typeCode,code);
    }
}
